package com.leetcode.algorithm.y22.m07.w4;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;

/**
 * 841. 钥匙和房间
 * 
 * https://leetcode.cn/problems/keys-and-rooms/
 * 
 * @author jie.deng
 *
 */
class Question0841Solution01 {

	// BFS
	public boolean canVisitAllRooms(List<List<Integer>> rooms) {
		Set<Integer> visitedSet = new HashSet<>();
		Queue<Integer> queue = new LinkedList<>();
		visitedSet.add(0);
		queue.offer(0);
		while (!queue.isEmpty()) {
			int len = queue.size();
			for (int i = 0; i < len; i++) {
				Integer roomNo = queue.poll();
				List<Integer> list = rooms.get(roomNo.intValue());
				for (Integer room : list) {
					if (visitedSet.add(room)) {
						queue.offer(room);
					}
				}
			}
		}

		return visitedSet.size() == rooms.size();
	}

}